Wrangle Data

espresso_summary <- 
  brewmaster_raw %>% 
  # Separate out name
  separate(name, sep = ",", into = c("type", "roastery", "origin")) %>%
  # parse json column
  my_brew_data_json(note) %>%
  # tidy decaf column
  mutate(decaf = case_when(is.na(decaf) ~ F, T ~ T)) %>%
  # tidy columns
  mutate(createdAt = as_date(createdAt), 
         roast_date = as_date(roast_date),
         age = createdAt - roast_date, ratio = `total weight`/dose) %>%
  # rename columns
  rename(total_time = `total time`, total_weight = `total weight`,
         mean_flow = `average flowrate`, brew_data = `brew data`)


flow_data <- 
  espresso_summary  %>%
  my_brew_data_series(brew_data, 
                      stall_tol = 0.1,
                      change_lag = 2) %>%
  # reshape data to have weight and time in a single column
  pivot_longer(cols = starts_with("t_"),
               names_to = "time", values_to = "weight") %>%
  # parse time and calc times
  mutate_if(is.character, trimws) %>%
  mutate(time = as.numeric(gsub("t_", "", time)) * 0.2,
         weight = as.numeric(weight),
         estimated_begin = estimated_first_flow * 0.2,
         estimated_end = estimated_last_flow * 0.2,
         flow_rate = (weight - dplyr::lag(weight))/0.2) %>%
  group_by(id) %>%
  mutate(smooth_flow = zoo::rollapply(flow_rate, 11, mean, fill = NA, na.rm = TRUE)) 

Weight Over Time

flow_data %>%
  filter(!decaf) %>%
  ggplot(aes(x = time, y = weight, group = id, colour = grind)) +
  geom_line() +
  scale_color_continuous(type = "viridis") +
  scale_x_continuous(labels = function(x)paste0(x,"s")) +
  scale_y_continuous("yield", labels = function(x)paste0(x, "g"))

Summary Stats

coffee_summary_extra <- 
  flow_data %>%
  group_by(id, grind, origin, roastery, dose, age) %>%
  summarise(max_entry = which.max(is.na(weight)),
            final_weight = max(weight, na.rm = T),
            estimated_begin = min(estimated_begin)) %>%
  mutate(final_time = max_entry * 0.2,
         age = as.numeric(age), 
         ratio = final_weight/dose) %>%
  filter(final_weight > 20, final_weight < 50)
coffee_summary_extra %>%
  ggplot(aes(final_time, grind, shape = origin, colour = age, size = ratio)) +
  geom_jitter(width = 0, height = 0.075) +
  scale_color_continuous(type = "viridis", labels = function(x) paste0(x, " dayss")) +
  guides(size = guide_legend(reverse = TRUE)) +
  scale_x_continuous("time", labels = function(x) paste0(x, "s"))

coffee_summary_extra %>%
  ggplot(aes(dose, final_weight, colour = origin)) +
  geom_point() +
  scale_x_continuous("dose", labels = function(x)paste0(x, "g")) +
  scale_y_continuous("yield", labels = function(x)paste0(x, "g"))

# coffee_summary_extra %>%
#   ggplot(aes(ratio, final_time, colour = origin, size = grind)) +
#   geom_point() 

coffee_summary_extra %>%
  ggplot(aes(final_time, ratio, colour = grind, size = age)) +
  scale_color_continuous(type = "viridis") +
  geom_point() +
  facet_wrap(~origin) +
  guides(size = guide_legend(reverse = TRUE)) +
  scale_size_continuous(labels = function(x) paste0(x, " days")) +
  scale_x_continuous("time", labels = function(x) paste0(x, "s"))

library(knitr)
library(kableExtra)
coffee_summary_extra %>%
  select(roastery, origin, age, grind, dose, yield = final_weight, ratio, time = final_time) %>%
  mutate(ratio = round(ratio, 2)) %>%
  group_by(roastery, origin) %>%
  summarise(mean_grind = mean(grind), mean_dose = mean(dose), mean_yield = mean(yield),
            mean_ratio = mean(ratio), mean_time = mean(time)) %>%
  arrange(roastery, origin) %>%
  kable(digits = 2) %>%
  kable_styling() 
roastery origin mean_grind mean_dose mean_yield mean_ratio mean_time
Cloud Picker Blend 2.64 18.00 38.39 2.13 31.28
Full Circle Brazil 3.47 17.84 36.23 2.03 31.39
Full Circle Guatemala 2.31 18.00 43.30 2.41 32.27
McCabes Blend 4.50 17.60 49.82 2.83 27.40
McCabes Brazil 4.18 14.68 44.25 3.05 29.65
McCabes Organic 4.20 13.50 45.87 3.40 22.60
Square Mile Blend 2.90 18.21 40.35 2.22 35.06
Upside Blend 3.95 18.00 38.05 2.11 28.27
Upside Brazil 4.00 18.03 37.30 2.07 28.52
Upside Rwanda 3.74 17.95 39.00 2.17 26.84

Flow Rate

flow_data %>%
  filter(smooth_flow > -2, smooth_flow < 8) %>%
  mutate(age = as.numeric(age)) %>%
  # # group_by(id, grind, origin, roastery, dose, age) %>%
  # mutate(flow_rate = (weight - dplyr::lag(weight))/0.2) %>%
  # group_by(id) %>%
  # mutate(smooth_flow = zoo::rollapply(flow, 11, mean, fill = NA, na.rm = TRUE)) %>%
  ggplot() +
  geom_line(aes(x = time, y = smooth_flow, group = id, colour = age)) +
  scale_color_continuous(type = "viridis") +
  scale_y_continuous("flow rate", labels = function(x) paste0(x, "g/s")) +
  geom_smooth(aes(x = time, y = smooth_flow), colour = "red")

Single Batch Over Time

single_batch <- 
  coffee_summary_extra %>%
  filter(origin == "Brazil", roastery == "Full Circle", grind > 3.4) %>%
  ggplot(aes(x = age, y = grind, size = ratio, colour = final_time, 
             group = 1, text = paste0("Dose: ", dose, 
                                      "<br>Yield: ", final_weight,
                                      "<br>Ratio: ", round(ratio, 2),
                                      "<br>Time: ", final_time))) +
  geom_jitter(width = 0, height = 0.01, alpha = 0.9) +
  scale_color_continuous(type = "viridis", labels = scales::number_format(suffix = "s")) +
  guides(size = guide_legend(reverse = TRUE)) +
  labs(x = "Days Since Roast", y = "Grind Setting", size = "Ratio", colour = "Shot Time")

plotly::ggplotly(single_batch, tooltip = "text")